// pages/answer /answer .js
import {addQuestionnaire,GetFatModel} from '../../api/api';
import * as echarts from '../../ec-canvas/echarts';
function initChart1(canvas, width, height, dpr) {
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // new
  });
  canvas.setChart(chart);

  var option = {
    backgroundColor: "transparent",
    // color: ["#37a2da", "#FF9F7F"],
    color: ["#8080f0"],
    tooltip: {
      show: true,
      trigger: 'axis',
      formatter: function (param) {
        console.log(param)
        return param.data.name+ '\n' +"胸围:"+param.data.value[0]+"cm"+ '\n' +"腰围:"+param.data.value[1]+"cm"+ '\n' +"臀围:"+param.data.value[2]+"cm"+ '\n' +"大腿围:"+param.data.value[3]+"cm"+ '\n' +"小腿围:"+param.data.value[4]+"cm"
      }
    },
    xAxis: {
      show: false
    },
    yAxis: {
      show: false
    },
    radar: {
      // shape: 'circle',
      splitArea: {
        areaStyle: {
          color: ["#f5f5f5", "#e5e1f7", "#f5f5f5", "#e5e1f7","#f5f5f5"]
          // color: ["#f5f5f5", "#f5f5f5", "#f5f5f5", "#f5f5f5","#f5f5f5"]
          // d6d0f6
          }
        },
        axisLine: {
            lineStyle: {
              color: "#d6d0f6"
            }
          },
        splitLine: {
          lineStyle: {
          color: "#d6d0f6"
          }
        },
        name: {
          // textStyle: {
          //     color: '#fff',
          //     backgroundColor: '#8080f0',
          //     borderRadius: 3,
          //     padding: [3, 5]
          // }
          textStyle: {
              color: '#8080f0',
              // backgroundColor: '#8080f0',
              // borderRadius: 3,
              // padding: [3, 5]
          }
      },
      indicator: [{
        // name: '胸围79.5~84.8',
        name: '胸围',
        max: 100
      },
      {
        // name: '腰围55.5~59.2',
        name: '腰围',
        max: 100
      },
      {
        // name: '臀部81~86.4',
        name: '臀部',
        max: 100
      },
      {
        // name: '大腿围46.8~49.4',
        name: '大腿围',
        max: 100
      },
      {
        // name: '小腿围28.1~59.6',
        name: '小腿围',
        max: 100
      },
      // {
      //   name: '门票',
      //   max: 500
      // }
      ]
    },
    series: [{
      name: '体围健康范围值',
      type: 'radar',
      tooltip: {
        trigger: 'item',
        position: 'right'
      },
      areaStyle: {
        // color: ["yellow"],
        // 透明度
        opacity: 0.4
        },
      data: [{
        value: [82.1, 57.4, 83.7, 48.1, 28.9],
        name: '标准值'
      },
      // {
      //   value: [79.5, 55.5, 81, 46.8, 28.1],
      //   name: '最小值'
      // }
      ],
      label: {
        normal: {
            // show: true,
            // formatter:function(params) {
            //     return params.value;
            // }
        }
    }
    }]
  };

  chart.setOption(option);
  return chart;
}
function initChart2(canvas, width, height, dpr) {
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // new
  });
  canvas.setChart(chart);

  var option = {
    backgroundColor: "transparent",
    // color: ["#37a2da", "#FF9F7F"],
    color: ["#8080f0"],
    tooltip: {
      show: true,
      trigger: 'axis',
      formatter: function (param) {
        console.log(param)
        return param.data.name+ '\n' +"胸围:"+param.data.value[0]+"cm"+ '\n' +"腰围:"+param.data.value[1]+"cm"+ '\n' +"臀围:"+param.data.value[2]+"cm"+ '\n' +"大腿围:"+param.data.value[3]+"cm"+ '\n' +"小腿围:"+param.data.value[4]+"cm"
      }
    },
    xAxis: {
      show: false
    },
    yAxis: {
      show: false
    },
    radar: {
      // shape: 'circle',
      splitArea: {
        areaStyle: {
          color: ["#f5f5f5", "#e5e1f7", "#f5f5f5", "#e5e1f7","#f5f5f5"]
          // color: ["#f5f5f5", "#f5f5f5", "#f5f5f5", "#f5f5f5","#f5f5f5"]
          // d6d0f6
          }
        },
        axisLine: {
            lineStyle: {
              color: "#d6d0f6"
            }
          },
        splitLine: {
          lineStyle: {
          color: "#d6d0f6"
          }
        },
        name: {
          // textStyle: {
          //     color: '#fff',
          //     backgroundColor: '#8080f0',
          //     borderRadius: 3,
          //     padding: [3, 5]
          // }
          textStyle: {
              color: '#8080f0',
              // backgroundColor: '#8080f0',
              // borderRadius: 3,
              // padding: [3, 5]
          }
      },
      indicator: [{
        // name: '胸围79.5~84.8',
        name: '胸围',
        max: 100
      },
      {
        // name: '腰围55.5~59.2',
        name: '腰围',
        max: 100
      },
      {
        // name: '臀部81~86.4',
        name: '臀部',
        max: 100
      },
      {
        // name: '大腿围46.8~49.4',
        name: '大腿围',
        max: 100
      },
      {
        // name: '小腿围28.1~59.6',
        name: '小腿围',
        max: 100
      },
      // {
      //   name: '门票',
      //   max: 500
      // }
      ]
    },
    series: [{
      name: '体围健康范围值',
      type: 'radar',
      tooltip: {
        trigger: 'item',
        position: 'right'
      },
      areaStyle: {
        // color: ["yellow"],
        // 透明度
        opacity: 0.4
        },
      data: [{
        value: [87.7, 61.3, 89.4, 50.9, 30.5],
        name: '标准值'
      },
      // {
      //   value: [79.5, 55.5, 81, 46.8, 28.1],
      //   name: '最小值'
      // }
      ],
      label: {
        normal: {
            // show: true,
            // formatter:function(params) {
            //     return params.value;
            // }
        }
    }
    }]
  };

  chart.setOption(option);
  return chart;
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    myTel:"",
    fatid:"",
    answerIndex:0,
    isCheckbox:false,
    illnessFalg:false,
    drugFalg:false,
    cacationFalg:false,
    diteNum:0,
    activityFalg:false,
    optionsData:[
      //疾病0
      [
        {
          name:"垂体瘤",
          checked:false,
        },
        {
          name:"皮质醇增多症",
          checked:false,
        },
        {
          name:"胰岛素瘤",
          checked:false,
        },
        {
          name:"Ⅱ型糖尿病",
          checked:false,
        },
        {
          name:"下丘脑肿瘤",
          checked:false,
        },
        {
          name:"甲状腺功能减低",
          checked:false,
        },
      ],
      //药物1
      [
        {
          name:"胰岛素",
          checked:false,
        },
        {
          name:"泼尼松",
          checked:false,
        },
        {
          name:"息斯敏",
          checked:false,
        },
        {
          name:"地塞米松",
          checked:false,
        },
        {
          name:"酚噻嗪",
          checked:false,
        },
        {
          name:"三环类抗抑郁药",
          checked:false,
        },
        {
          name:"氯丙嗪",
          checked:false,
        },
        {
          name:"促蛋白合成制剂",
          checked:false,
        },
        {
          name:"可的松",
          checked:false,
        },
        {
          name:"肾上腺皮质激素",
          checked:false,
        },
      ],
      //饮食2
      [
        {
          name:"喜爱甜食类",
          checked:false,
        },
        {
          name:"喜爱米食类",
          checked:false,
        },
        {
          name:"早餐常不吃",
          checked:false,
        },
        {
          name:"常吃得过饱",
          checked:false,
        },
        {
          name:"睡前进食",
          checked:false,
        },
        {
          name:"蔬果吃得少",
          checked:false,
        },
        {
          name:"胃口较好",
          checked:false,
        },
        {
          name:"饥饿感较强",
          checked:false,
        },
        {
          name:"口味偏重/咸",
          checked:false,
        },
        {
          name:"饮食较油腻",
          checked:false,
        },
        {
          name:"经常吃零食",
          checked:false,
        },
        {
          name:"三餐不规律",
          checked:false,
        },
      ],
      //排便3
      [
        {
          name:"经常性腹泻",
          checked:false,
        },
        {
          name:"易消化不良/腹胀",
          checked:false,
        },
        {
          name:"大便干结/干硬",
          checked:false,
        },
        {
          name:"排便费力/排不出",
          checked:false,
        },
        {
          name:"次数<3次/周",
          checked:false,
        },
        {
          name:"排不净/意犹未尽",
          checked:false,
        },
      ],
      //活动4
      [
        {
          name:"工作久坐为主/或长期久坐",
          checked:false,
        },
        {
          name:"久坐时间>8小时/天",
          checked:false,
        },
        {
          name:"常持续2小时无改变坐姿",
          checked:false,
        },
        {
          name:"长时间站立或坐、躺为主",
          checked:false,
        },
        {
          name:"每周<150分钟中等强度运动",
          checked:false,
        },
        {
          name:"基本无运动量",
          checked:false,
        },
      ],
      //健康诉求5
      [
        {
          name:"增肌",
          checked:false,
        },
        {
          name:"局部塑性",
          checked:false,
        },
        {
          name:"增重",
          checked:false,
        },
        {
          name:"减脂",
          checked:false,
        },
        {
          name:"膳食指导",
          checked:false,
        },
        {
          name:"运动指导",
          checked:false,
        },
        {
          name:"调理肠道",
          checked:false,
        },
        {
          name:"改善睡眠",
          checked:false,
        },
      ],

    ],
    firstStageval:"",
    secondlyStageval:"",
    // 问题
    question0:"",
    question1:"",
    question2:"",
    question3:"",
    question4:"",
    question5:"",
    question6:"",
    question7:"",
    //诉求
    appealNum:0,
    // 尺子
    value:0,//身高
    value2: 0,//体重
    styles: [{
      line: '#d5d5d5',
      bginner: '#ffffff',
      bgoutside: '#d5d5d5',
      lineSelect: '#8080f0',
      font: '#c0c0c0'
    }, {
      line: '#d5d5d5',
      bginner: '#ffffff',
      bgoutside: '#d5d5d5',
      lineSelect: '#8080f0',
      font: '#c0c0c0'
    }],
    // 雷达图
    ec1: {
      onInit: initChart1
    },
    ec2: {
      onInit: initChart2
    },
    bmiValue:0
  },

  //活动情况确认
  activityComfirm(){
    let activityNum=0;
    let {optionsData}=this.data;
    this.saveData();
    optionsData[4].forEach((value)=>{
        if (value.checked) {
          activityNum++;
        }
    })
    if (activityNum>=3) {
      this.setData({
        answerIndex:19
      })
      wx.setStorageSync("tzcFatType",`久坐少动型肥胖#${this.data.answerIndex}`);
    }else{
      this.setData({
        answerIndex:13
      })
      wx.setStorageSync("tzcFatType",`饮食型肥胖+久坐少动型肥胖#${this.data.answerIndex}`);
    }
  },
  //排便情况确认
  cacationComfirm(){
    let cacationNum=0;
    let {optionsData}=this.data;
    this.saveData();
    optionsData[3].forEach((value)=>{
        if (value.checked) {
          cacationNum++;
        }
    })
    if (cacationNum>=3) {
      this.setData({
        answerIndex:18
      })
      wx.setStorageSync("tzcFatType",`肠菌型肥胖#${this.data.answerIndex}`);
    }else{
      this.setData({
        answerIndex:12
      })
      wx.setStorageSync("tzcFatType",`饮食型肥胖+肠菌型肥胖#${this.data.answerIndex}`);
      
    }
  },
  //判断bmi
  bmiJudge:function(){
    let {value,value2}=this.data;
    let bmi=(value2/Math.pow(value/100,2)).toFixed(1);
    wx.setStorageSync("tzcBmi", bmi);
    this.setData({
      bmiValue:bmi
    })
    if (bmi>=20) {
        this.setData({
          answerIndex:1
        })
    }else{
      this.setData({
        answerIndex:16
      })
    }

  }, 
  // 尺子的值控制
  bindvalue: function(e) {
    // console.log(e)
    this.setData({
      value: e.detail.value
    })
  },
  bindvalue2: function(e) {
    // console.log(e)
    this.setData({
      value2: e.detail.value
    })
  },
  // 第一题
  firstStageToggle(e){
    let index= e.target.dataset.index;
      this.setData({
        firstStageval:index
      })
    // if (index==0) {
    //   this.setData({
    //     firstStageval:2
    //   })
    // }else{
    //   this.setData({
    //     firstStageval:1
    //   })
    // }
    
  },
  firstStageComfirm(){
    if (this.data.firstStageval=="") {
        return;
    }
      if (this.data.firstStageval==1) {
          this.setData({
            answerIndex:3,
            question1:"是"
          })
      }else{
        this.setData({
          answerIndex:2,
          question1:"否"
        })
      }
  },
  // 第二题
  secondlyStageToggle(e){
    let index= e.target.dataset.index;
      this.setData({
        secondlyStageval:index
      })
    // if (index==0) {
    //   this.setData({
    //     secondlyStageval:2
    //   })
    // }else{
    //   this.setData({
    //     secondlyStageval:1
    //   })
    // }
    
  },
  secondlyStageComfirm(){
    if (this.data.secondlyStageval=="") {
      return;
    }
      if (this.data.secondlyStageval==1) {
          this.setData({
            answerIndex:8,
             question2:"是"
          })
          this.saveData();
          wx.setStorageSync("tzcFatType",`遗传性肥胖#${this.data.answerIndex}`);
      }else{
        this.setData({
          answerIndex:3,
           question2:"否"
        })
      }
  },
  // 开始测评
  firstNextPage(){
    if(this.data.isCheckbox){
      this.setData({
        answerIndex:1
      })
    }
  }, 
  // 选框切换
  chaeckboxChange(e){
    if (e.detail.value.length!=0) {
      this.setData({
        isCheckbox:true
      })
    }else{
      this.setData({
        isCheckbox:false
      })
    }
  },
  //疾病选项
  illnessToggle(e){
    let index=e.target.dataset.index;
    let data=this.data.optionsData[0];
    let illnessFalg=null;
    let question3=""
    data.forEach((item,key)=>{
      if (index==key) {
          item.checked=!item.checked;
      }
      if (item.checked) {
          illnessFalg=true;
          if (question3=="") {
            question3=item.name
          }else{
            question3=question3+','+item.name
          }
      }
    })
    this.setData({
      "optionsData[0]":data,
      illnessFalg,
      question3
    })
  },
  // 药物选项
  drugToggle(e){
    let index=e.target.dataset.index;
    let data=this.data.optionsData[1];
    let drugFalg=null;
    let question4="";
    data.forEach((item,key)=>{
      if (index==key) {
          item.checked=!item.checked;
      }
      if (item.checked) {
          drugFalg=true;
          if (question4=="") {
            question4=item.name
          }else{
            question4=question4+','+item.name
          }
      }
    })
    this.setData({
      "optionsData[1]":data,
      drugFalg,
      question4
    })
  },
  // 饮食选项
  dietToggle(e){
    let index=e.target.dataset.index;
    let data=this.data.optionsData[2];
    let dietFalg=null;
    let diteNum=0;
    let question5="";
    data.forEach((item,key)=>{
      if (index==key) {
          item.checked=!item.checked;
      }
      if (item.checked) {
          dietFalg=true;
          diteNum++;
          if (question5=="") {
            question5=item.name
          }else{
            question5=question5+','+item.name
          }
      }
    })
    this.setData({
      "optionsData[2]":data,
      dietFalg,
      diteNum,
      question5
    })
  },
  //排便选项
  cacationToggle(e){
    let index=e.target.dataset.index;
    let data=this.data.optionsData[3];
    let cacationFalg=null;
    let question6="";
    data.forEach((item,key)=>{
      if (index==key) {
          item.checked=!item.checked;
      }
      if (item.checked) {
          cacationFalg=true;
          if (question6=="") {
            question6=item.name
          }else{
            question6=question6+','+item.name
          }
      }
    })
    this.setData({
      "optionsData[3]":data,
      cacationFalg,
      question6
    })
  },
  //活动选项
  activityToggle(e){
    let index=e.target.dataset.index;
    let data=this.data.optionsData[4];
    let activityFalg=null;
    let question7="";
    data.forEach((item,key)=>{
      if (index==key) {
          item.checked=!item.checked;
      }
      if (item.checked) {
          activityFalg=true;
          if (question7=="") {
            question7=item.name
          }else{
            question7=question7+','+item.name
          }
      }
    })
    this.setData({
      "optionsData[4]":data,
      activityFalg,
      question7
    })
  },
  //饮食确认
  dietConfirm(){
    let {diteNum}=this.data;
    if (diteNum=="0") {
      this.setData({
        question5:"以上都没有"
      })
      return;
    }
    if(diteNum>=5){
      this.setData({
        answerIndex:11
      })
      this.saveData();
      wx.setStorageSync("tzcFatType",`饮食型肥胖#${this.data.answerIndex}`);
    }else{
      this.setData({
        answerIndex:6,
      })
    }
  },
  // 诉求选项
  appealToggle(e){
    let index=e.target.dataset.index;
    let appealNum=0;
    let data=this.data.optionsData[5];
    let question0="";
    data.forEach((item,key)=>{
      if (index==key) {
        item.checked=!item.checked;
      }
      if (item.checked) {
        appealNum++;
          if (question0=="") {
            question0=item.name
          }else{
            question0=question0+','+item.name
          }
      }
    })
    this.setData({
      "optionsData[5]":data,
      appealNum,
      question0
    })
  },
  // 诉求确定
  appealComfirm(){
    if (this.data.appealNum) {
       this.saveData();
        this.setData({
          answerIndex:17
        })
        if(this.data.bmiValue>18.5){
          wx.setStorageSync("tzcFatType",`正常体重#${this.data.answerIndex}`);
        }else{
          wx.setStorageSync("tzcFatType",`偏瘦体重#${this.data.answerIndex}`);
        }
     

    }
  },
  // 点击确定或以上都没有
  nextPage(e){
    let answerIndex=e.currentTarget.dataset.val;
    this.setData({
      answerIndex
    })
    if (answerIndex==14) {
      wx.showLoading({
        title: '加载中',
        mask:true
      })
      setTimeout(function () {
        wx.hideLoading()
      }, 600)
    }
    // 判断是不是以上都没有
    switch (answerIndex) {
      //4 5 7 11
      case "4":
        this.setData({
          question3:"以上都没有"
        })
        break;
      case "5":
        this.setData({
          question4:"以上都没有"
        })
        break;
      case "7":
        this.setData({
          question6:"以上都没有"
        })
        break;
      case "11":
        this.setData({
          question7:"以上都没有"
        })
        break;
    }

    //判断是否要保存数据
    if(answerIndex>7&&answerIndex<=13){
      // 判断是什么数据肥胖类型
      switch (answerIndex) {
        case "8":
          wx.setStorageSync("tzcFatType",`遗传性肥胖#${this.data.answerIndex}`);
          break;
        case "9":
          wx.setStorageSync("tzcFatType",`继发性肥胖#${this.data.answerIndex}`);
          break;
        case "10":
          wx.setStorageSync("tzcFatType",`药物性肥胖#${this.data.answerIndex}`);
          break;
        case "11":
          wx.setStorageSync("tzcFatType",`饮食型肥胖#${this.data.answerIndex}`);
          break;
        case "12":
          wx.setStorageSync("tzcFatType",`饮食型肥胖+肠菌型肥胖#${this.data.answerIndex}`);
          break;
        case "13":
          wx.setStorageSync("tzcFatType",`饮食型肥胖+久坐少动型肥胖#${this.data.answerIndex}`);
          break;
      }
      this.saveData();
    }
    if (answerIndex==18||answerIndex==19) {
      switch (answerIndex) {
        case "18":
          wx.setStorageSync("tzcFatType",`肠菌型肥胖#${this.data.answerIndex}`);
          break;
        case "19":
          wx.setStorageSync("tzcFatType",`久坐少动型肥胖#${this.data.answerIndex}`);
          break;
      
  
      }
      this.saveData();
    }
  

  },
  //保存数据
  saveData(){
    let {question1,question2,question3,question4,question5,question6,question7,question0,value,value2}=this.data;
    // console.log("question1",question1,"question2",question2,"question3",question3,"question4",question4,"question5",question5,"question6",question6,"question7",question7);
    // // console.log("保存数据");
      let data={
        fatid:this.data.fatid,
        question1,question2,question3,question4,question5,question6,question7,question0,
        height:value,
        weight:value2
      }
    addQuestionnaire(data,(e)=>{
      console.log(e);
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      let answerIndex=options.page;
      
      if (answerIndex) {
        let bmiValue=wx.getStorageSync("tzcBmi");
        this.setData({
          answerIndex,
          bmiValue
        })
      }
 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //判断是否登录了
    let TZCphone=wx.getStorageSync('TZCphone');
    let openid=wx.getStorageSync('TZCopenid');
    if (!openid) {
      openid=""
    }    

    let getdata={
          phone:TZCphone,
          openid:openid
        }
    if(!TZCphone){
      wx.showModal({
        title: '提示',
        content: '你还没登录，请先登录',
        showCancel:false,
        success (res) {
          if (res.confirm) {
            wx.switchTab({
              url: '../my/my'
            })
          } 
        }
      })
      return;
    }else{
      // this.updateStatusMessage('请先登录');
      this.setData({
        myTel:TZCphone,
      })
      GetFatModel(getdata,(e)=>{
        console.log(e.data.id);
        this.setData({
            fatid:e.data.id
        })
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})